#include "MWCharSet.h"

#include <windows.h>

namespace Myway
{

	int CharSet::AnsiToUnicode(wchar_t * o, int size, const char * i)
	{
	#ifdef MW_PLATFORM_WIN32
		int len = MultiByteToWideChar(CP_ACP, 0, i, -1, NULL, 0);

		if (o == NULL)
			return len;

		len = MultiByteToWideChar(CP_ACP, 0, i, -1, o, len);

		return len;
	#endif
	}

	int CharSet::UnicodeToAnsi(char * o, int size, const wchar_t * i)
	{
	#ifdef MW_PLATFORM_WIN32
		int len = WideCharToMultiByte(CP_ACP, 0, i, -1, NULL, 0, NULL, NULL);

		if (o == NULL)
			return len;

		len = WideCharToMultiByte(CP_ACP, 0, i, -1, o, len, NULL, NULL);

		return len;
	#endif
	}

	int CharSet::Utf8ToAnsi(char * o, int size, const char * i)
	{
	#ifdef MW_PLATFORM_WIN32
		wchar_t buffer[2048];

		// convert to wide char
		int len = MultiByteToWideChar(CP_UTF8, 0, i, -1, NULL,0);

		d_assert (len < 2048);

		MultiByteToWideChar(CP_UTF8, 0, i, -1, buffer, len);

		// convert to ansi
		len = WideCharToMultiByte(CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL);

		if (o == NULL)
			return len;

	   d_assert (len < size);

		len = WideCharToMultiByte(CP_ACP, 0, buffer, -1, o, len, NULL, NULL);

		return len;
	#endif
	}

	int CharSet::Utf8ToUnicode(wchar_t * o, int size, const char * i)
	{
	#ifdef MW_PLATFORM_WIN32
		int len = MultiByteToWideChar(CP_UTF8, 0, i, -1, NULL,0);

		if (o == NULL)
			return len;

		d_assert (len < size);

		len = MultiByteToWideChar(CP_UTF8, 0, i, -1, o, len);

		return len;
	#endif
	}

}